#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-12-7
# software: PyCharm

import socket

# 创建UDP套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 服务器地址和端口
server_address = ('localhost', 9999)

while True:
    message = input('请输入要发送的消息: ')
    client_socket.sendto(message.encode('utf-8'), server_address)
    if message.lower() == 'exit':
        break
    # 接收服务器回复的消息，最多接收1024字节
    data, server_address = client_socket.recvfrom(1024)
    if data:
        print(f'从服务器接收到回复消息: {data.decode("utf-8")}')
